# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=openmp
pkgver=17.0.6
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="LLVM OpenMP Runtime Library"
url="https://openmp.llvm.org/"
# s390x: LIBOMP_ARCH = UnknownArchitecture
# armhf: doesn't match arm baseline
arch="all !armhf !s390x"
license="Apache-2.0"
depends_dev="
	$pkgname=$pkgver-r$pkgrel
	"
makedepends="
	clang
	cmake
	elfutils-dev
	libffi-dev
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	perl
	samurai
	"
checkdepends="llvm$_llvmver-test-utils"
subpackages="$pkgname-dev"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/openmp-${pkgver//_/}.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/cmake-${pkgver//_/}.src.tar.xz
	"
builddir="$srcdir/$pkgname-${pkgver//_/}.src"
options="!check" # todo

case "$CARCH" in
aarch64|ppc64le|x86_64)
	depends_dev="
		$depends_dev
		$pkgname-bitcode=$pkgver-r$pkgrel
		"
	subpackages="
		$subpackages
		$pkgname-bitcode
		libomptarget
		libomptarget-rtl-cuda
		libomptarget-rtl-amdgpu
		libomptarget-rtl
		"
	;;
riscv64)
	depends_dev="
		$depends_dev
		$pkgname-bitcode=$pkgver-r$pkgrel
		"
	subpackages="
		$subpackages
		$pkgname-bitcode
		libomptarget
	"
esac

prepare() {
	default_prepare
	mv "$srcdir"/cmake-${pkgver//_/}.src "$srcdir"/cmake
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	CC=clang \
	CXX=clang++ \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIBOMP_INSTALL_ALIASES=OFF \
		-DCLANG_TOOL=/usr/bin/clang \
		-DLINK_TOOL=/usr/bin/llvm-link \
		-DOPT_TOOL=/usr/bin/opt \
		-DPACKAGER_TOOL=/usr/bin/clang-offload-packager \
		-DOPENMP_LLVM_TOOLS_DIR=/usr/lib/llvm$_llvmver/bin \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	cmake --build build --target check-openmp
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	rm -f "$pkgdir"/usr/lib/libarcher_static.a
}

libomptarget() {
	amove usr/lib/libomptarget.so.*
}

rtl() {
	amove usr/lib/libomptarget.rtl.*.so.*
}

amdgpu() {
	amove usr/lib/libomptarget.rtl.amdgpu.so.*
}

cuda() {
	amove usr/lib/libomptarget.rtl.cuda.so.*
}

bitcode() {
	amove usr/lib/libomptarget*.bc
}

sha512sums="
836c48db873e3da64835913c2c1d80efaebdfc7061f153acc7b478ea0d73b9c546a09d70dd1465e3e92684947c4a9cb197886b29cbd42418a778faa56b08cc5e  openmp-17.0.6.src.tar.xz
b2c5e404ca36542d44e1a7f2801bbcecbcf5f1e8c63b793bb2308228483406bdfe002720aadb913c0228cd2bbe5998465eaadc4a49fad6a5eb6ff907fa5fd45a  cmake-17.0.6.src.tar.xz
"
